package wvtools.extend;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import edu.udo.cs.wvtool.generic.loader.UniversalLoader;
import edu.udo.cs.wvtool.generic.loader.WVTDocumentLoader;
import edu.udo.cs.wvtool.main.WVTDocumentInfo;
import edu.udo.cs.wvtool.util.WVToolException;
import edu.udo.cs.wvtool.util.WVToolIOException;

public class XMLDocumentLoader implements WVTDocumentLoader
{
    private InputStream     inputStream   = null;
    private UniversalLoader defaultLoader = new UniversalLoader();

    @Override
    public InputStream loadDocument(WVTDocumentInfo d) throws WVToolException
    {
        if (d instanceof WVTXMLDocumentInfo)
        {
            inputStream = new ByteArrayInputStream(((WVTXMLDocumentInfo) d)
                    .getContext().getBytes());
            return inputStream;
        }
        return defaultLoader.loadDocument(d);
    }

    @Override
    public void close(WVTDocumentInfo d) throws WVToolException
    {
        try
        {
            if (inputStream != null)
                inputStream.close();
        }
        catch (IOException e)
        {
            throw new WVToolIOException("Could not close strean.", e);
        }
    }
}
